package com.c.ctgproject.ui.other

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import cn.jpush.android.api.JPushInterface
import com.c.ctgproject.BuildConfig
import com.c.ctgproject.R
import com.c.ctgproject.base.CTGproject
import com.c.ctgproject.base.MyBaseApplication
import com.c.ctgproject.entity.UpdateEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserver
import com.c.ctgproject.http.ObserverListener
import com.c.ctgproject.ui.login.ChangePasswordActivity
import com.c.ctgproject.ui.login.LoginActivity
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.dialog.CommonAlertDialog
import com.c.ctgproject.wiget.dialog.UpdateDialog
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.title.*
import okhttp3.ResponseBody
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2020/9/28
 *描述: 设置页面
 */
class SettingActivity : AppCompatActivity(), View.OnClickListener {
    private var builder: AlertDialog.Builder? = null
    private val statusColor: StatusColor = StatusColor()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        StatusBarUtil.setTranslucent(this)
        statusColor.setAndroidNativeLightStatusBar(this, false)
        setSupportActionBar(toolbar)
        toolbar_title.text = "设置"
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        event()
    }

    private fun event() {
        //版本检测
        val code = "v${packageManager.getPackageInfo(packageName, PackageManager.GET_CONFIGURATIONS).versionName}"
        mVersionTV.text = code
        userName.text=MyBaseApplication.userName
        loginout.setOnClickListener(this)
        //修改密码
        mChangePasswordRL.setOnClickListener {
            startActivity<ChangePasswordActivity>()
        }
        //版本检测
        mCheckVersionRL.setOnClickListener {
//            var versionName = this.packageManager.getPackageInfo(this.packageName, 0).versionName
//            versionName = "v$versionName"
//            ApiMethods.checkUpdate(MyObserver(this, versionListener), versionName)
        }
//        //退出登录
//        mLoginOutRL.setOnClickListener {
//            userToken = ""
//            sp.edit().clear().apply()
//            startActivity<LoginActivity>()
//        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        //退出按钮
        if (v == loginout) {
            CommonAlertDialog(this,"确定退出登录吗"){
                //去除别名
                JPushInterface.deleteAlias(this, currentAlias)
                //去除标签
                JPushInterface.deleteTags(this, 1000, currentSet)
                Utils.setShare2(applicationContext, "loginstatu", "1")
                //退出登录清除所有sp缓存
                val shared = getSharedPreferences("ShareData", 0).edit()
                userToken = ""
                shared.clear().apply()
                val intent = Intent(applicationContext, LoginActivity::class.java)
                startActivity(intent)
                finish()
            }.show()
//            builder = AlertDialog.Builder(this@SettingActivity).setTitle("提示！")
//                .setMessage("确定退出登录吗？").setPositiveButton("退出？") { _, _ ->
//                    //去除别名
//                    JPushInterface.deleteAlias(this, currentAlias)
//                    //去除标签
//                    JPushInterface.deleteTags(this, 1000, currentSet)
//                    Utils.setShare2(applicationContext, "loginstatu", "1")
//                    //退出登录清除所有sp缓存
//                    val shared = getSharedPreferences("ShareData", 0).edit()
//                    userToken = ""
//                    shared.clear().apply()
//                    val intent = Intent(applicationContext, LoginActivity::class.java)
//                    startActivity(intent)
//                    finish()
//                }.setNegativeButton("取消") { dialogInterface, _ ->
//                    dialogInterface.dismiss()
//                }
//            builder!!.create().show()
        }
    }

    private val versionListener = object : ObserverListener {
        override fun onNext(t: ResponseBody) {
            val result = t.string().handlerNull()
            val entity = getGson().fromJson(result, UpdateEntity::class.java)
            if (entity.status == 0) {
                if (entity.data?.forceUpdate == null) {
                    CustomToast.showToast(this@SettingActivity, "当前已是最新版本")
                    return
                }
                if (!CTGproject.getBaseApplication().isDownloadApk) {
                    //修复更新
                    AlertDialog.Builder(this@SettingActivity)
                        .setTitle("更新")
                        .setMessage(entity.data?.versionDesc)
                        .setPositiveButton("下载安装") { _, _ ->
                            UpdateDialog(this@SettingActivity, R.style.MillionDialogStyle, entity.data?.versionUrl.handlerNull()).show()
                        }
                        .setNegativeButton("取消") { _, _ ->
                        }.show()
                }
            } else {
                CustomToast.showToast(this@SettingActivity, entity.msg.handlerNull())
            }

        }

        override fun onError(error: String) {
            CustomToast.showToast(this@SettingActivity, error)

        }
    }
}